package net.yla.smartbus.core.constants;

import com.alibaba.fastjson.annotation.JSONType;
import net.yla.smartbus.core.exception.EnumNotFoundException;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 下发命令枚举
 * @author KongXianDe
 * @date 20190806
 **/
@JSONType(serializeEnumAsJavaBean = true)
public enum InstructEnum {

    CHANGE_OPERATE_STATUS(0, "切换运营状态"),
    SEND_LED_INFO(1, "发布LED信息"),
    SEND_DISPATCH_INFO(2, "发送调度信息"),
    SEND_CAMERA_SNAP(3, "发送抓拍计划"),
    SEND_AT_COMMAND(4, "发送终端参数"),
    SEND_CTRL_PARA(5, "发送控制参数"),
    DEVICE_UPDATE(6, "终端升级"),
    PARA_CONFIG(7, "参数配置"),
    PARA_TPMS(8, "胎压参数"),
    TERMINAL_CHECK(9, "终端自检"),
    TERMINAL_RESET(10, "终端重启"),
    ROUTE_DIRECT(11, "更新车辆路线");

    private int code;
    private String desc;
    private String name;

    InstructEnum() {
    }

    private InstructEnum(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    InstructEnum(int code, String desc, String name) {
        this.code = code;
        this.desc = desc;
        this.name = name;
    }

    private static final Map<Integer, InstructEnum> BY_VALUE_MAP = new LinkedHashMap<>();

    static {
        for (InstructEnum yesNoEnum : InstructEnum.values()) {
            BY_VALUE_MAP.put(yesNoEnum.getCode(), yesNoEnum);
        }
    }

    /**
     * 根据code查询对应的外卖类型枚举
     * @param code dode
     * @return YesNoEnum
     * @throws EnumNotFoundException
     */
    public static InstructEnum convertCode2Enum(int code) throws EnumNotFoundException {
        if (!BY_VALUE_MAP.containsKey(code)) {
            throw new EnumNotFoundException("枚举对应的code" + code + "不存在");
        }
        return BY_VALUE_MAP.get(code);
    }

    /**
     * 根据枚举name查询对应的外卖类型枚举
     * @param str 枚举的name
     * @return YesNoEnum
     * @throws EnumNotFoundException
     */
    public static InstructEnum convertStr2Enum(String str) throws EnumNotFoundException {
        try {
            return InstructEnum.valueOf(str);
        } catch (Exception e) {
            throw new EnumNotFoundException("枚举对应常量" + str + "不存在");
        }
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public String getName() {
        return this.name();
    }

    public void setName(String name) {
        this.name = name;
    }}
